package com.atguigu.qqzone.basic;

import com.atguigu.qqzone.exceptions.DAOException;
import com.atguigu.qqzone.pojo.Topic;
import com.atguigu.qqzone.pojo.UserBasic;
import com.atguigu.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: BasicDAO
 * @Description:
 * @Author: wty
 * @Date: 2022/12/5
 */

public class BasicDAO<T> {
    private QueryRunner queryRunner = new QueryRunner();

    /**
     * @param
     * @return int
     * @description //TODO
     * @param: sqlStr
     * @param: params
     * @date 2022/12/4 17:53
     * @author wty
     **/
    public int dml(String sqlStr, Object... params) {
        Connection connection = null;

        int affectedrows = 0;

        try {
            connection = DruidUtils.getConnection();
            affectedrows = queryRunner.update(connection, sqlStr, params);

        } catch (SQLException e) {
            e.printStackTrace();
            throw new DAOException("BasicDAO dml出错!");
        } finally {
            DruidUtils.close(null, null, connection);
        }
        return affectedrows;

    }

    /**
     * @return java.util.List<T>
     * @description 返回多行结果
     * @param: sqlStr
     * @param: clazz
     * @param: params
     * @date 2022/12/4 17:59
     * @author wty
     **/
    public List<T> queryMulti(String sqlStr, Class<T> clazz, Object... params) {
        Connection connection = null;
        List<T> list = null;

        try {
            connection = DruidUtils.getConnection();
            list = queryRunner.query(connection, sqlStr, new BeanListHandler<T>(clazz), params);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DAOException("BasicDAO queryMulti出错!");
        } finally {
            DruidUtils.close(null, null, connection);
        }
        return list;
    }

    /**
     * @param
     * @return T
     * @description 返回单行多列数据
     * @param: sqlStr
     * @param: clazz
     * @param: params
     * @date 2022/12/4 18:01
     * @author wty
     **/
    public T querySingle(String sqlStr, Class<T> clazz, Object... params) {
        Connection connection = null;
        T t = null;
        try {
            connection = DruidUtils.getConnection();
            t = queryRunner.query(connection, sqlStr, new BeanHandler<T>(clazz), params);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DAOException("BasicDAO querySingle出错!");
        } finally {
            DruidUtils.close(null, null, connection);
        }
        return t;
    }

    public Object queryScalar(String sqlStr, Object... params) {
        Connection connection = null;
        Object o = null;

        try {
            connection = DruidUtils.getConnection();
            o = queryRunner.query(connection, sqlStr, new ScalarHandler(), params);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DAOException("BasicDAO queryScalar出错!");
        } finally {
            DruidUtils.close(null, null, connection);
        }
        return o;
    }

    /**
     * @return java.util.List<T>
     * @description 返回多行结果--用于多表查询
     * @param: sqlStr
     * @param: clazz
     * @param: params
     * @date 2022/12/4 17:59
     * @author wty
     **/
    public List<Topic> queryMultiListAndMap(String sqlStr, List<Topic> list, Object... params) {
        Connection connection = null;

        try {
            connection = DruidUtils.getConnection();
            MapListHandler mapListHandler = new MapListHandler();
            List<Map<String, Object>> query = queryRunner.query(connection, sqlStr, mapListHandler, params);

            for (Map<String, Object> map : query) {
                UserBasic userBasic = new UserBasic();
                Topic topic = new Topic();

                userBasic.setId((Integer) map.get("author"));

                topic.setAuthor(userBasic);
                topic.setId((Integer) map.get("id"));
                topic.setTitle((String) map.get("title"));
                topic.setContent((String) map.get("content"));
                topic.setTopicDate((java.util.Date) map.get("topicDate"));

                list.add(topic);
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new DAOException("BasicDAO queryMultiListAndMap出错!");
        } finally {
            DruidUtils.close(null, null, connection);
        }
        return list;
    }
}
